const express = require('express')
const models = require('../models')
const router = express.Router()
const { success } = require('../utils/message')

// 首页接口
router.get('/', async function (req, res, next) {
    console.log(111111)
    // 广告位
    const adverts = await models.Advert.findAll({
        order: [['sort', 'ASC']]
    })

    // 所有分类
    const categories = await models.Category.findAll({
        where: { parentId: 0 },
        order: [['sort', 'ASC']]
    })

    // 家中常备(30分钟送药到家)
    const commonUses = await models.Product.findAll({
        where: {
            commonUse: true,
            onsale: true
        },
        order: [['id', 'DESC']]
    })

    // 防疫专区
    const preventions = await models.Product.findAll({
        where: {
            prevention: true,
            onsale: true
        },
        order: [['id', 'DESC']]
    })

    // 特惠折扣
    const discounts = await models.Product.findAll({
        where: {
            discount: true,
            onsale: true
        },
        order: [['id', 'DESC']]
    })

    // 品牌
    const brands = await models.Brand.findAll({
        where: {
            enable: true
        },
        order: [['id', 'DESC']]
    })

    // 推荐商品
    const recommends = await models.Product.findAll({
        where: {
            recommend: true,
            onsale: true
        },
        order: [['id', 'DESC']]
    })

    // 应急、滋补、心脑血管，后续完成

    success(res, '查询成功', { adverts, categories, commonUses, preventions, discounts, brands, recommends })
})

module.exports = router
